package com.resou.reader.utils;

import android.content.Context;
import android.os.Build;
import android.support.annotation.LayoutRes;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.view.FlowLayout;

import java.util.Arrays;
import java.util.List;

public class ViewHelper {
    public static View createView(ViewGroup parent, @LayoutRes int layoutId) {
        return LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
    }

    /**
     * 创建View视图
     *
     * @param content
     * @param layoutId
     * @return
     */
    public static View createView(Context content, @LayoutRes int layoutId) {
        return LayoutInflater.from(content).inflate(layoutId, null);
    }


    /**
     * 为一个容器動態添加标签
     *
     * @param context      上下文
     * @param linearLayout 容器
     * @param params       标签集合  注意 ：这里的集合是一个字符串 用逗号（，）分割；
     */
    public static void setLabelView(Context context, FlowLayout linearLayout, String params) {
        if (null == params) {
            return;
        }
        if (null == linearLayout) {
            return;
        }
        String[] list = params.split(",");
        for (int i = 0, length = list.length; i < length; i++) {
            TextView textView = new TextView(context);
            textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setText(list[i]);
            textView.setTextSize(9f);
            textView.setCompoundDrawablePadding(10);
            textView.setTextColor(context.getResources().getColor(R.color.white));
            textView.setIncludeFontPadding(false);
            textView.setBackgroundResource(R.drawable.shap_booklistlabel);
            textView.setPadding(20, 10, 20, 10);
            textView.setEnabled(true);
            textView.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams vl = (LinearLayout.LayoutParams) textView.getLayoutParams();
            vl.setMargins(10, 5, 10, 5);
            textView.setLayoutParams(vl);
            linearLayout.addView(textView);
        }
    }

    /**
     * 为一个容器動態添加标签
     *
     * @param context      上下文
     * @param linearLayout 容器
     * @param params       标签集合  注意 ：这里的集合是一个字符串 用逗号（，）分割；
     */
    public static void setLabelViewSolid(Context context, FlowLayout linearLayout, String params) {
        if (null == params) {
            throw new NullPointerException("params is NULL");
        }
        if (null == linearLayout) {
            throw new NullPointerException("View  is  NULL");
        }
        String[] list = params.split(",");
        for (int i = 0, length = list.length; i < length; i++) {
            TextView textView = new TextView(context);
            textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setText(list[i]);
            textView.setTextSize(9f);
            textView.setCompoundDrawablePadding(10);
            textView.setTextColor(context.getResources().getColor(R.color.white));
            textView.setIncludeFontPadding(false);
            textView.setBackgroundResource(R.drawable.shap_tabview);
            textView.setPadding(10, 5, 10, 5);
            textView.setEnabled(true);
            textView.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams vl = (LinearLayout.LayoutParams) textView.getLayoutParams();
            vl.setMargins(10, 5, 10, 5);
            textView.setLayoutParams(vl);
            linearLayout.addView(textView);
        }
    }
}
